namespace SmartSoft.Lists
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormDmTsLister : FormListLister
    {
        private IContainer components;
        private bool ib_CheckEdited;
        private bool ib_KeyCtrlF3;
        private string is_FixedAssetType = "C";

        public FormDmTsLister()
        {
            this.is_FixedAssetType = "C";
            this.FirstFilter = "Loai_Ts = 'C'";
            this.InitializeComponent();
        }

        public FormDmTsLister(string _FixedAssetType)
        {
            this.is_FixedAssetType = _FixedAssetType;
            this.FirstFilter = "Loai_Ts = '" + _FixedAssetType + "'";
            this.InitializeComponent();
        }

        public FormDmTsLister(FormListLister po_ParentForm) : base(po_ParentForm)
        {
            this.is_FixedAssetType = "C";
            this.FirstFilter = "Loai_Ts = 'C'";
            this.InitializeComponent();            
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            base.SuspendLayout();
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x2bd, 0x10a);
            base.LocalFieldCount = 0;
            base.LocalFieldName = "MA_TS";
            base.LocalTableName = "DMTS";
            base.Name = "FormDmTsLister";
            base.OrderFieldNames = "MA_TS";
            this.Text = "Danh mục tài sản";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public override bool OnAdd()
        {
            if (!this.CheckEdited)
            {
                base.LocalFormEditor = new FormDmTsEditor(is_FixedAssetType);
                (base.LocalFormEditor as FormDmTsEditor).FixedAssetType = this.FixedAssetType;
            }
            return base.OnAdd();
        }

        public override bool OnEdit()
        {
            if (!this.CheckEdited)
            {
                if (this.ib_KeyCtrlF3)
                {
                    base.LocalFormEditor = new FormDmTs3Editor();
                    this.ib_KeyCtrlF3 = false;
                }
                else
                {
                    base.LocalFormEditor = new FormDmTsEditor(is_FixedAssetType);
                    (base.LocalFormEditor as FormDmTsEditor).FixedAssetType = this.FixedAssetType;
                }
            }
            else if (this.ib_KeyCtrlF3)
            {
                base.LocalFormEditor = new FormDmTsBpEditor();
            }
            else
            {
                base.LocalFormEditor = new FormDmTs0Editor();
            }
            return base.OnEdit();
        }

        public override void OnFillData()
        {
            object[][] objArray = new object[17][];
            object[] objArray2 = new object[4];
            objArray2[0] = "Ma_Ts";
            objArray2[1] = "Mã tài sản";
            objArray2[2] = "C";
            objArray[0] = objArray2;
            object[] objArray3 = new object[4];
            objArray3[0] = "Ten_Ts";
            objArray3[1] = "Tên tài sản";
            objArray3[2] = "C";
            objArray[1] = objArray3;
            object[] objArray4 = new object[4];
            objArray4[0] = "Dvt";
            objArray4[1] = "Đvt";
            objArray4[2] = "C";
            objArray[2] = objArray4;
            objArray[3] = new object[] { "TTien0", "Nguyên giá", "N", AppInstance.TextNumericFormat.LocalAmount };
            objArray[4] = new object[] { "TTien1", "Đã khấu hao", "N", AppInstance.TextNumericFormat.LocalAmount };
            objArray[5] = new object[] { "TTien3", "Còn lại", "N", AppInstance.TextNumericFormat.LocalAmount };
            object[] objArray8 = new object[4];
            objArray8[0] = "Nuoc_Sx";
            objArray8[1] = "Nước sản xuất";
            objArray8[2] = "C";
            objArray[6] = objArray8;
            object[] objArray9 = new object[4];
            objArray9[0] = "Nam_Sx";
            objArray9[1] = "Năm sản xuất";
            objArray9[2] = "C";
            objArray[7] = objArray9;
            object[] objArray10 = new object[4];
            objArray10[0] = "Cong_Suat";
            objArray10[1] = "Cấu hình";
            objArray10[2] = "C";
            objArray[8] = objArray10;
            objArray[9] = new object[] { "So_Luong", "Số lượng", "N", AppInstance.TextNumericFormat.Quantity };
            object[] objArray12 = new object[4];
            objArray12[0] = "Tinh_Kh";
            objArray12[1] = "Tính khấu hao";
            objArray12[2] = "C";
            objArray[10] = objArray12;
            objArray[11] = new object[] { "TTien4", "Giá trị tính khấu hao", "N", AppInstance.TextNumericFormat.LocalAmount };
            objArray[12] = new object[] { "TTien2", "Khấu hao tháng", "N", AppInstance.TextNumericFormat.LocalAmount };
            object[] objArray15 = new object[4];
            objArray15[0] = "Ma_MDSD";
            objArray15[1] = "Mục đích";
            objArray15[2] = "C";
            objArray[13] = objArray15;
            object[] objArray16 = new object[4];
            objArray16[0] = "Ma_Nh_Ts";
            objArray16[1] = "Mã nhóm";
            objArray16[2] = "C";
            objArray[14] = objArray16;
            object[] objArray17 = new object[4];
            objArray17[0] = "Tk";
            objArray17[1] = "Tài khoản";
            objArray17[2] = "C";
            objArray[15] = objArray17;
            object[] objArray18 = new object[4];
            objArray18[0] = "Loai_Ts";
            objArray18[1] = "Loại Ts";
            objArray18[2] = "C";
            objArray[16] = objArray18; 
            base.ColNames = objArray;
            base.OnFillData();
        }

        public override void OnRefresh()
        {
            DmTsDataAccess access = new DmTsDataAccess();
            if (base.IsChildForm)
            {
                if (base.ParentFormLister != null)
                {
                    base.LocalDataSet = access.GetDataSetByParent(AppInstance.Subsidiary, (base.ParentFormLister as FormDmNhTsLister).FixedAssetType, base.ParentCurrentRow[base.ParentFieldName].ToString());
                }
                else
                {
                    base.LocalDataSet = access.GetDataSetByParent(AppInstance.Subsidiary, this.FixedAssetType, base.ParentCurrentRow[base.ParentFieldName].ToString());
                }
            }
            else
            {
                base.LocalDataSet = access.GetDataSet(AppInstance.Subsidiary, this.FixedAssetType);
            }
            base.OnRefresh();
            this.FirstFilter = this.FirstFilter;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.F3))
            {
                this.ib_KeyCtrlF3 = true;
                this.OnEdit();
                return true;
            }
            if ((this.CheckEdited && (keyData == (Keys.Control | Keys.F6))) && (base.LocalCurrentRow != null))
            {
                FormDmTsGEditor editor = new FormDmTsGEditor();
                editor.MasterKey = base.LocalCurrentRow["Stt"];
                editor.Show();
            }
            this.ib_KeyCtrlF3 = false;
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public bool CheckEdited
        {
            get
            {
                return this.ib_CheckEdited;
            }
            set
            {
                this.ib_CheckEdited = value;
                if (this.ib_CheckEdited)
                {
                    base.ShowAddButton = false;
                    base.ShowDeleteButton = false;
                    base.ShowMergeButton = false;
                }
            }
        }

        public string FixedAssetType
        {
            get
            {
                return this.is_FixedAssetType;
            }
            set
            {
                this.is_FixedAssetType = value;
            }
        }

        public override void DoFilter()
        {
            if (FirstFilter.Equals("Loai_Ts = 'C'"))
            {
                this.Text = "Danh mục tài sản";
                this.LocalGridBrowser.Columns[0].HeaderText = "Mã tài sản";
                this.LocalGridBrowser.Columns[1].HeaderText = "Tên tài sản";
                this.FixedAssetType = "C";
            }
            else if (FirstFilter.Equals("Loai_Ts = 'K'"))
            {
                this.Text = "Danh mục công cụ";
                this.LocalGridBrowser.Columns[0].HeaderText = "Mã công cụ";
                this.LocalGridBrowser.Columns[1].HeaderText = "Tên công cụ";
                this.FixedAssetType = "K";
            }
            base.DoFilter();
        }
    }
}

